import { Image as VanImage } from "vant";
import { scrollMixin } from "../../../utils/mixin";
export default {
  name: "",
  //import引入的组件需要注入到对象中才能使用
  components: { VanImage },
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      coursesId: "",
      chatStrId: "",
      answerData: [],
      isLoadMore: true,
      total_page: 0,
      page: 1
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getRecordList() {
      let query = {
        page: this.page,
        courses_id: Number(this.coursesId)
      };
      if (this.fun.getKey("client") === "work" || window.localStorage.getItem("client")) {
        query = { ...this.getConfig, ...query, chat_str_id: this.chatStrId };
      }
      $http.post("plugin.wechat-video-courses.frontend.controller.manage.record.reward", query, "加载中..").then(response => {
        if (response.result == 1) {
          let res = response.data.reward_list;
          let basicInfo = response.basic_info;
          this.isLoadMore = true;
          this.total_page = res.last_page;
          if (!this.total_page) {
            this.page = 0;
          }
          this.answerData = res.data;
          this.$nextTick(() => {
            this.fun.wxShare(
              "",
              { mid: this.fun.getKeyByMid() },
              {
                link: window.location.href,
                title: basicInfo?.home.mailInfo?.name,
                imgUrl: basicInfo?.home.mailInfo?.logo_url,
                description: ""
              }
            );
          }, 50);
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        let query = {
          page: this.page,
          courses_id: Number(this.coursesId)
        };
        if (this.fun.getKey("client") === "work" || window.localStorage.getItem("client")) {
          query = { ...this.getConfig, ...query, chat_str_id: this.chatStrId };
        }
        $http.post("plugin.wechat-video-courses.frontend.controller.manage.record.reward", query, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let data = res.data;
              let answerData = data.reward_list.data;
              this.answerData = [...this.answerData, ...answerData]; //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    }
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    if (this.fun.getKey("client") === "work" || window.localStorage.getItem("client")) {
      this.chatStrId = window.localStorage.getItem("chat_id") || "";
    }
    if (this.$route.params.id) {
      this.coursesId = this.$route.params.id;
      this.getRecordList();
    }
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  activated() {} //如果页面有keep-alive缓存功能，这个函数会触发
};
